import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:flutter_uxin/Tools/Define/Define.dart';
import 'package:flutter_uxin/addressBook/Parsers/commonAddressBook.dart';

class chatScreenPage extends StatefulWidget {
  chatScreenPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _chatScreenPageState createState() => _chatScreenPageState();
}

class _chatScreenPageState extends State<chatScreenPage> {

    List   messageList;

    void initState() {
      super.initState();
    }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class LeftChatCellWidget extends StatelessWidget {
  final String headPortrait;
  final String name;
  final String time;
  final String text;
  final String mediaURL;
  final String mediaTime;
  final MediaType mediaType;

  @required
  VoidCallback clickHeadPortrait;
  VoidCallback clickMedia;

  LeftChatCellWidget(
      {this.headPortrait,
      this.name,
      this.time,
      this.text,
      this.mediaURL,
      this.mediaTime,
      this.mediaType,
      this.clickHeadPortrait,
      this.clickMedia});
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          height: 80,
          child: _leftChatDetailsWidget(
              headPortrait: headPortrait,
              name: name,
              text: text,
              mediaURL: mediaURL,
              mediaTime: mediaTime,
              mediaType: mediaType,
              clickHeadPortrait: clickHeadPortrait,
              clickMedia: clickMedia),
        ),
        Container(
          color: Color(0xffeaeaea),
          constraints: BoxConstraints.expand(height: 1.0),
        ),
      ],
    ));
  }

  Widget _leftChatDetailsWidget(
      {String headPortrait,
      String name,
      String text,
      String mediaURL,
      String mediaTime,
      MediaType mediaType,
      VoidCallback clickHeadPortrait,
      VoidCallback clickMedia}) {
    return Container(
      padding: EdgeInsets.only(
        left: 5,
        right: 5,
      ),
      child: Row(
        children: <Widget>[
          Flexible(
            flex: 1,
            child: ListView(
              padding: const EdgeInsets.all(8),
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(left: 16),
                  child: CachedNetworkImage(
                    imageUrl:
                        "https://static001.geekbang.org/resource/image/7d/eb/7d5a36a8727ed68f241719b7768262eb.jpg",
                    placeholder: (context, url) => CircularProgressIndicator(),
                    errorWidget: (context, url, error) => Icon(Icons.error),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(left: 16),
                  child: Text(
                    name,
                    style: TextStyle(fontSize: 16),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 7,
            child: _detailsWidget(
                text: text,
                mediaURL: mediaURL,
                mediaTime: mediaTime,
                mediaType: mediaType,
                clickHeadPortrait: clickHeadPortrait,
                clickMedia: clickMedia),
          ),
        ],
      ),
    );
  }



}

Widget _detailsWidget(
    {String text,
      String mediaURL,
      String mediaTime,
      MediaType mediaType,
      VoidCallback clickHeadPortrait,
      VoidCallback clickMedia}) {
  if (mediaType == MediaType.textType) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      child: Text(
        text,
        style: TextStyle(fontSize: 16),
      ),
    );
  } else if (mediaType == MediaType.pictureType) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      child: GestureDetector(
          child: CachedNetworkImage(
            imageUrl:
            "https://static001.geekbang.org/resource/image/7d/eb/7d5a36a8727ed68f241719b7768262eb.jpg",
            width: 150,
            height: 150,
            placeholder: (context, url) => CircularProgressIndicator(),
            errorWidget: (context, url, error) => Icon(Icons.error),
          ),
          onTap: () {
            print('mediaURL-------------$mediaURL');
          }),
    );
  } else if (mediaType == MediaType.voiceType) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      child: IconButton(
        icon: Icon(
          Icons.mode_comment,
          size: 20,
          color: Color(0xFF80D8FF),
        ),
        onPressed: () {
          clickMedia();
        },
      ),
    );
  } else if (mediaType == MediaType.videoType) {
    return _videoWidget(
        mediaURL: mediaURL, mediaTime: mediaTime, clickMedia: clickMedia);
  } else {
    return Container();
  }
}


Widget _videoWidget(
    {String mediaURL, String mediaTime, VoidCallback clickMedia}) {
  return Container(
    padding: EdgeInsets.only(left: 10),
    child: GestureDetector(
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          CachedNetworkImage(
            imageUrl:
            "https://static001.geekbang.org/resource/image/7d/eb/7d5a36a8727ed68f241719b7768262eb.jpg",
            width: 150,
            height: 150,
            placeholder: (context, url) => CircularProgressIndicator(),
            errorWidget: (context, url, error) => Icon(Icons.error),
          ),
          Icon(
            Icons.play_circle_filled,
            size: 40,
            color: Color(0xFF80D8FF),
          ),
        ],
      ),
      onTap: () {
        clickMedia();
        print('mediaURL-------------$mediaURL');
      },
    ),
  );
//
}